﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract VrstaRaspodjele typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class VrstaRaspodjeleDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<VrstaRaspodjeleDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.VrstaRaspodjele _currentVrstaRaspodjele = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxVrstaRaspodjeleDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxVrstaRaspodjeleErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxVrstaRaspodjeleBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Naziv property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNazivDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.VrstaRaspodjele> _VrstaRaspodjeleList;
				
		/// <summary> 
		/// The list of VrstaRaspodjele to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.VrstaRaspodjele> VrstaRaspodjeleList
		{
			get {return this._VrstaRaspodjeleList;}
			set
			{
				this._VrstaRaspodjeleList = value;
				this.uxVrstaRaspodjeleBindingSource.DataSource = null;
				this.uxVrstaRaspodjeleBindingSource.DataSource = value;
				this.uxVrstaRaspodjeleDataGridView.DataSource = null;
				this.uxVrstaRaspodjeleDataGridView.DataSource = this.uxVrstaRaspodjeleBindingSource;				
				//this.uxVrstaRaspodjeleBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxVrstaRaspodjeleBindingSource_ListChanged);
				this.uxVrstaRaspodjeleBindingSource.CurrentItemChanged += new System.EventHandler(OnVrstaRaspodjeleBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnVrstaRaspodjeleBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentVrstaRaspodjele = uxVrstaRaspodjeleBindingSource.Current as Entities.VrstaRaspodjele;
			
			if (_currentVrstaRaspodjele != null)
			{
				_currentVrstaRaspodjele.Validate();
			}
			//_VrstaRaspodjele.Validate();
			OnCurrentEntityChanged();
		}

		//void uxVrstaRaspodjeleBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.VrstaRaspodjele"/> instance.
		/// </summary>
		public Entities.VrstaRaspodjele SelectedVrstaRaspodjele
		{
			get {return this._currentVrstaRaspodjele;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxVrstaRaspodjeleDataGridView.VirtualMode;}
			set
			{
				this.uxVrstaRaspodjeleDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxVrstaRaspodjeleDataGridView.AllowUserToAddRows;}
			set {this.uxVrstaRaspodjeleDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxVrstaRaspodjeleDataGridView.AllowUserToDeleteRows;}
			set {this.uxVrstaRaspodjeleDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxVrstaRaspodjeleDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxVrstaRaspodjeleDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="VrstaRaspodjeleDataGridViewBase"/> class.
		/// </summary>
		public VrstaRaspodjeleDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxVrstaRaspodjeleDataGridView = new System.Windows.Forms.DataGridView();
			this.uxVrstaRaspodjeleBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxVrstaRaspodjeleErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNazivDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxVrstaRaspodjeleErrorProvider
			// 
			this.uxVrstaRaspodjeleErrorProvider.ContainerControl = this;
			this.uxVrstaRaspodjeleErrorProvider.DataSource = this.uxVrstaRaspodjeleBindingSource;						
			// 
			// uxVrstaRaspodjeleDataGridView
			// 
			this.uxVrstaRaspodjeleDataGridView.AutoGenerateColumns = false;
			this.uxVrstaRaspodjeleDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxVrstaRaspodjeleDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxNazivDataGridViewColumn			});
			this.uxVrstaRaspodjeleDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxVrstaRaspodjeleDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxVrstaRaspodjeleDataGridView.Name = "uxVrstaRaspodjeleDataGridView";
			this.uxVrstaRaspodjeleDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxVrstaRaspodjeleDataGridView.TabIndex = 0;	
			this.uxVrstaRaspodjeleDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxVrstaRaspodjeleDataGridView.EnableHeadersVisualStyles = false;
			this.uxVrstaRaspodjeleDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnVrstaRaspodjeleDataGridViewDataError);
			this.uxVrstaRaspodjeleDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVrstaRaspodjeleDataGridViewCellValueNeeded);
			this.uxVrstaRaspodjeleDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnVrstaRaspodjeleDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxNazivDataGridViewColumn
			//
			this.uxNazivDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNazivDataGridViewColumn.DataPropertyName = "Naziv";
			this.uxNazivDataGridViewColumn.HeaderText = "Naziv";
			this.uxNazivDataGridViewColumn.Name = "uxNazivDataGridViewColumn";
			this.uxNazivDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNazivDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNazivDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxVrstaRaspodjeleDataGridView);
			this.Name = "VrstaRaspodjeleDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxVrstaRaspodjeleBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				VrstaRaspodjeleDataGridViewEventArgs args = new VrstaRaspodjeleDataGridViewEventArgs();
				args.VrstaRaspodjele = _currentVrstaRaspodjele;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class VrstaRaspodjeleDataGridViewEventArgs : System.EventArgs
		{
			private Entities.VrstaRaspodjele	_VrstaRaspodjele;
	
			/// <summary>
			/// the  Entities.VrstaRaspodjele instance.
			/// </summary>
			public Entities.VrstaRaspodjele VrstaRaspodjele
			{
				get { return _VrstaRaspodjele; }
				set { _VrstaRaspodjele = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxVrstaRaspodjeleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnVrstaRaspodjeleDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxVrstaRaspodjeleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVrstaRaspodjeleDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVrstaRaspodjeleDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = VrstaRaspodjeleList[e.RowIndex].ID;
						break;
					case "uxNazivDataGridViewColumn":
						e.Value = VrstaRaspodjeleList[e.RowIndex].Naziv;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxVrstaRaspodjeleDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnVrstaRaspodjeleDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxVrstaRaspodjeleDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						VrstaRaspodjeleList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxNazivDataGridViewColumn":
						VrstaRaspodjeleList[e.RowIndex].Naziv = (System.String)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
